package com.mxchip.livestarmobile.mobile.ui.call

import android.os.Looper
import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import com.mxchip.livestarmobile.mobile.util.Log.MyLog

object AVRepository {

    val mIsEnter = MutableLiveData<Boolean>(false)
    private val mJoinedUserIds = MutableLiveData<List<String>>(arrayListOf())

    private val mUserIds = MediatorLiveData<List<String>>().apply {
        value = arrayListOf()
    }

    private val mJoinedMicUserIds = MutableLiveData<List<String>>(arrayListOf())

    private val mMicUserIds = MediatorLiveData<List<String>>().apply {
        value = arrayListOf()
    }

    private val mJoinedAVDevices = MutableLiveData<List<AVDevice>>(arrayListOf())

    private val mAVDevices = MediatorLiveData<List<AVDevice>>().apply {
        value = arrayListOf()
    }


    private val avDevicesObserver = Observer<Any> {
        if (mIsEnter.value != null
                && mIsEnter.value == true) {
              MyLog.logD(MyLog.AV_DEVICE_MODEL,"VRepository:: avDevicesObserver\n ${mJoinedAVDevices.value}")
            mAVDevices.value = mJoinedAVDevices.value
        } else {
            mAVDevices.value = arrayListOf()
        }
    }


    private val userIdsObserver = Observer<Any> {
        if (mIsEnter.value != null
                && mIsEnter.value == true) {
            mUserIds.value = mJoinedUserIds.value
        } else {
            mUserIds.value = null
        }
    }

    private val micUserIdsObserver = Observer<Any> {
        if (mIsEnter.value != null
                && mIsEnter.value == true) {
            mMicUserIds.value = mJoinedMicUserIds.value
        } else {
            mMicUserIds.value = arrayListOf()
        }
    }


    init {
        mAVDevices.addSource(mJoinedAVDevices, avDevicesObserver)
        mAVDevices.addSource(mIsEnter, avDevicesObserver)

        mUserIds.addSource(mJoinedUserIds, userIdsObserver)
        mUserIds.addSource(mIsEnter, userIdsObserver)

        mMicUserIds.addSource(mJoinedMicUserIds, micUserIdsObserver)
        mMicUserIds.addSource(mIsEnter, micUserIdsObserver)

    }



    fun addAVDevice(device: AVDevice) {

        val list = mJoinedAVDevices.value

        Log.e("AVRepository", "before list: $list")

        if (list is ArrayList) {
            list.add(device)
        }
        if (Looper.myLooper() == Looper.getMainLooper()) {
            mJoinedAVDevices.value = list
        } else {
            mJoinedAVDevices.postValue(list)
        }
        Log.e("AVRepository", "after list: $list")

    }

    fun removeAVDevice(uid: String) {
        val list = mJoinedAVDevices.value?.filter {
            uid != it.uid
        }
        if (Looper.myLooper() == Looper.getMainLooper()) {
            mJoinedAVDevices.value = list
        } else {
            mJoinedAVDevices.postValue(list)
        }

          MyLog.logD(MyLog.AV_DEVICE_MODEL,"VRepository::removeAVDevice $list")
    }

    fun resetDevices(devices: List<AVDevice>) {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            mJoinedAVDevices.value = devices
        } else {
            mJoinedAVDevices.postValue(devices)
        }

    }


    fun removeAVDevice(uid: String, deviceName: String) {
        val list = mJoinedAVDevices.value?.filterNot {
            uid == it.uid && deviceName == it.deviceName
        }
        if (Looper.myLooper() == Looper.getMainLooper()) {
            mJoinedAVDevices.value = list
        } else {
            mJoinedAVDevices.postValue(list)
        }
          MyLog.logD(MyLog.AV_DEVICE_MODEL,"VRepository::removeAVDevice $list")
    }

    fun addUserId(uid: String) {

        val list = mJoinedUserIds.value

        if (list is ArrayList) {
            list.add(uid)
        }
        if (Looper.myLooper() == Looper.getMainLooper()) {
            mJoinedUserIds.value = list
        } else {
            mJoinedUserIds.postValue(list)
        }

    }

    fun removeUserId(uid: String) {
//        removeMicUserId(uid)
          MyLog.logD(MyLog.AV_DEVICE_MODEL,"VRepository::removeUserId $uid")
//        removeAVDevice(uid)

        val list = mJoinedUserIds.value
        if (list is ArrayList) {
            list.remove(uid)
        }
          MyLog.logD(MyLog.AV_DEVICE_MODEL,"removeUserId list:$list---->")
        if (Looper.myLooper() == Looper.getMainLooper()) {
            mJoinedUserIds.value = list
        } else {
            mJoinedUserIds.postValue(list)
        }
    }


    fun addMicUserId(uid: String) {
        val list = mJoinedMicUserIds.value
        if (list is ArrayList) {
            list.add(uid)
        }
        if (Looper.myLooper() == Looper.getMainLooper()) {
            mJoinedMicUserIds.value = list
        } else {
            mJoinedMicUserIds.postValue(list)
        }

    }

    fun removeMicUserId(uid: String) {
        val list = mJoinedMicUserIds.value
        if (list is ArrayList) {
            list.remove(uid)
        }
        if (Looper.myLooper() == Looper.getMainLooper()) {
            mJoinedMicUserIds.value = list
        } else {
            mJoinedMicUserIds.postValue(list)
        }

    }


    val avDevices: LiveData<List<AVDevice>>
        get() = mAVDevices

    val userIds: LiveData<List<String>>
        get() = mUserIds

    val micUserIds: LiveData<List<String>>
        get() = mMicUserIds

    val joinedAVDevices: LiveData<List<AVDevice>>
        get() = mJoinedAVDevices

    fun enterAfterJoined() {
        mIsEnter.value = true
    }

/*    fun isEnter(): LiveData<Boolean> {
        return isEnter
    }*/

    fun clear() {
        mIsEnter.value = false
        mJoinedUserIds.value = arrayListOf()
        mJoinedMicUserIds.value = arrayListOf()
        mJoinedAVDevices.value = arrayListOf()
    }

}